import { DressUpData } from "../data/DressUpData";
import { ItemLogicBase } from "../../logicItem/ItemLogicBase";
import { GameTool } from "../../tool/GameTool";
import { MDressUpAvatarMgr } from "../MDressUpAvatarMgr";
import { DressUpServerDataMgr, DressUpServerTag } from "../data/DressUpServerDataMgr";
import { DressUpDataParse } from "../DressUpDataParse";
import { MDressUpTabDataMgr } from "../MDressUpTabDataMgr";

/**
 * 套装部件item
 */
export class LotteryTabItemLogic extends ItemLogicBase {


    private bgImg: Laya.Image;
    private lineImg: Laya.Image;
    private iconImg: Laya.Image;
    private descBtn: Laya.Image;
    private arrowBtn: Laya.Image;
    private itemBox: Laya.Image;
    private goodsDescLab: Laya.Label;

    private colorBox:Laya.Image;

    private clickHandler:Laya.Handler;
    private clickFaceHandler:Laya.Handler;

    private dataMgr:MDressUpTabDataMgr;
    
    private isWear:boolean;
    
    constructor(box: Laya.Sprite){
        super(box);
    }
    initNode(box: Laya.Box): void {

        let itemBox = box.getChildByName("itemBox") as Laya.Image;
        this.itemBox = itemBox;
        this.bgImg = itemBox.getChildByName("bgImg") as Laya.Image;
        this.lineImg = itemBox.getChildByName("lineImg") as Laya.Image;
        this.iconImg = itemBox.getChildByName("iconImg") as Laya.Image;
        this.descBtn = itemBox.getChildByName("descBtn") as Laya.Image;
        this.arrowBtn = itemBox.getChildByName("arrowBtn") as Laya.Image;
        this.goodsDescLab = itemBox.getChildByName("goodsDescLab") as Laya.Label;
        this.iconImg.size(50,50);
        this.iconImg.left = 18;
        this.box.on(Laya.Event.CLICK, this ,this.onClick);
    }

    initUi(data: any[]) {
        this.data = data;

        let goodsData: any = this.data[0];
        let isFace:boolean = false;
        this.clickHandler = this.data[1];
        this.clickFaceHandler = this.data[2];
        this.dataMgr = this.data[3];
        this.isWear = false;
        this.goodsDescLab.text = goodsData.preform_desc;
        this.arrowBtn.off(Laya.Event.CLICK, this, this.onClickFaceItem);
        
        if(goodsData.define_id > 0){
            isFace = DressUpServerDataMgr.Ins.containTagByKey("脸部_"+DressUpServerTag.AVATAR_BONE_TAG, goodsData.system_tag_ids);
            if(isFace){
                this.arrowBtn.scaleY = this.dataMgr.isOpen?-1:1;
                this.arrowBtn.on(Laya.Event.CLICK, this, this.onClickFaceItem);
            }
            this.setLotteryUI(goodsData);
        }else{

            this.setDressUpUI(goodsData);
            this.setGoodsState(goodsData, goodsData.buynum>0 && !goodsData.isfacedressUp);
        }
        this.arrowBtn.visible = isFace;
        // this.imgsp.skin = "comp/image.png";
    }

    private onClickFaceItem(e:Laya.Event){
        e.stopPropagation();
        this.dataMgr.isOpen = !this.dataMgr.isOpen;
        this.arrowBtn.scaleY = this.dataMgr.isOpen?-1:1;
        this.clickFaceHandler && this.clickFaceHandler.runWith([this.data[0],this.dataMgr.isOpen]);
    }
    
    private setGoodsState(goodsData:any, own:boolean){
        if (own && !goodsData.isfacedressUp) {
            // 拥有
            this.isWear = MDressUpAvatarMgr.Ins.isWear(goodsData.preform_id);
            let url = this.isWear?"pageres/roleCenterpage/xuanz(2).png":"pageres/roleCenterpage/put.png";
            GameTool.setSkin(url, this.bgImg);
            GameTool.setSkin("pageres/roleCenterpage/shu.png", this.lineImg);
            this.box.alpha = 1;
            this.bgImg.alpha = 1;
            this.lineImg.height = this.isWear?58:62;
        } else {
            // 不存在
            GameTool.setSkin("pageres/roleCenterpage/put.png", this.bgImg);
            this.bgImg.alpha = 0.4;
            GameTool.setSkin("pageres/roleCenterpage/zx.png", this.lineImg);
            this.box.alpha = 0.6;
            this.lineImg.height = 62;
        }
    }
    
    // private isShowPrice(goodsData:DressUpData){
    //     return goodsData && !goodsData.buynum && !goodsData.developer_avatar_json;
    // }

    private setLotteryUI(goodsData:any){
        this.itemBox.width = 280;
        this.itemBox.x = 0;
        this.goodsDescLab.text = goodsData.goods_desc;
        GameTool.setSkin(goodsData.goods_icon_url, this.iconImg, true);
        this.setGoodsState(goodsData, goodsData.is_has > 0);
    }

    private setDressUpUI(goodsData:DressUpData){
        if(! this.iconImg || this.iconImg.destroyed) return;
        // if(goodsData.color && goodsData.color.length){
        //     this.iconImg.visible = false;
        //     this.colorBox && (this.colorBox.visible = true);
        //     this.colorBox = GameTool.createCricleColor(50, GameTool.getRGBStr(goodsData.color), this.colorBox);
        //     this.box.addChild(this.colorBox);
        //     this.colorBox.left = 20;
        //     this.colorBox.centerY = 0;
        // }else{
            this.iconImg.visible = true;
            this.colorBox && (this.colorBox.visible = false);
            let url = goodsData.iconUrl || DressUpDataParse.getPlatformImgUrl(goodsData);
            if((<any>goodsData).isfacedressUp){
                url = DressUpDataParse.getProductUrl(goodsData) || "";
            }
            this.box.width = 280;
            this.itemBox.width = (<any>goodsData).isfacedressUp?260:280;
            this.itemBox.x = (<any>goodsData).isfacedressUp?20:0;
            GameTool.setSkin(url, this.iconImg, true);
        // }
    }

    onClick(){
        console.log("点击");
        if(this.data[0] && this.data[0].is_has > 0){
            this.isWear = !this.isWear;
            this.clickHandler&&this.clickHandler.runWith([this.data[0], this.isWear]);
            this.setGoodsState(this.data[0], this.data[0].is_has > 0);
        }
    }


    clear(): void {
        this.descBtn.off(Laya.Event.CLICK, this ,this.onClick);
        this.clickHandler = this.iconImg = null;
        this.bgImg = null;
        this.lineImg = null;
        this.clickFaceHandler = null;
        this.goodsDescLab = null;
        this.descBtn = null;
        this.box = null;
        this.data = null;
    }
}